package engine.systems.render.pen
{
	import ash.core.Entity;
	
	import ash.core.Component;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	
	public class Pen extends Component
	{
		internal var invalidate:Boolean = false;
		
		public var drawing:Boolean = false;
		public var erase:Boolean = false;
		public var circle:Boolean = true;
		
		private var _data:BitmapData = new BitmapData(1, 1, true, 0);
		public var color:uint;
		public var radius:Number;
		
		public function Pen(radius:Number = 5, color:uint = 0xFFFFFFFF, width:int = 1, height:int = 1)
		{
			this.radius = radius;
			this.color = color;
			
			this.resize(width, height);
		}
		
		override public function destroy():void
		{
			this._data.dispose();
			this._data = null;
		}
		
		public function get data():BitmapData { return _data; }
		
		public function resize(width:int, height:int):void
		{
			if(width < 1) 	width = 1;
			if(height < 1) 	height = 1;
			
			if(_data.width == width && _data.height == height) return;
			
			this.recalculate(width, height);
		}
		
		public function get width():int { return _data.width; }
		public function set width(width:int):void
		{
			if(width < 1) width = 1;
			
			if(width == _data.width) return;
			
			this.recalculate(width, _data.height);
		}
		
		public function get height():int { return _data.height; }
		public function set height(height:int):void
		{
			if(height < 1) height = 1;
			
			if(height == _data.height) return;
			
			this.recalculate(_data.width, height);
		}
		
		private function recalculate(width:int, height:int):void
		{
			_data.dispose();
			_data = null;
			_data = new BitmapData(width, height, true, 0);
			
			this.invalidate = true;
		}
	}
}